Imports System.Data.SqlClient
Imports System.Drawing
Imports DevExpress.XtraGrid
Imports DevExpress.XtraGrid.Views.Base
Imports DevExpress.XtraGrid.Columns
Imports DevExpress.XtraEditors
Imports DevExpress.XtraScheduler
Imports DevExpress.XtraScheduler.UI
'--- Created Date On : 12-Oct-2010 (Gyan)
'--- Modifyed Date   : 12-Oct-2010(Priyabrat)
'--- Modifyed Date   : 02-Nov-2010(Nirmal)
'--- Modified Date   : 04-Dec-2010(Jagdish) For Revenue by Demographics Report by age group and Occupation wise

Public Class frmDiagnosisFrequency

    Private mstrReportType As String

    Public Sub New(ByVal strRerportType As String)
        'This call is required by the Windows Form Designer.
        InitializeComponent()
        'Add any initialization after the InitializeComponent() call
        mstrReportType = strRerportType
    End Sub

    Private Sub frmPatientStatement_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If e.KeyCode = Keys.Escape Then
            Me.Close()
        End If
    End Sub

    Private Sub frmPatientStatement_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            dteFromDate.EditValue = Format(DateAdd(DateInterval.Month, (-1), Today), gstrDtFormat)
            dteToDate.EditValue = Format(Today, gstrDtFormat)
            If mstrReportType = "Revenue By Demographics" Then
                Call rgpDemographics_SelectedIndexChanged(Nothing, Nothing)
                rgpDemographics.Visible = True
            Else
                Call PopulateCheckedListboxDX(chklistCauses, "Diagnosis", "DiagnosisId", "LtblDiagnosis", "DiagnosisId,Diagnosis", "", "Diagnosis")
            End If
            Me.Text = mstrReportType
        Catch ex As Exception
            MessageBox.Show(ex.Message, gstrMessage, MessageBoxButtons.OK, MessageBoxIcon.Error)
            Call AddErrorLog("Error " & Err.Number & " (" & Err.Description & ") in Procedure " & _
                 Reflection.MethodBase.GetCurrentMethod.Name & " Of " & Me.Name)
        End Try
    End Sub

    Private Sub btnViewReport_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnViewReport.Click
        Try
            If IsValidEntry() = False Then Exit Sub

            Dim i As Integer
            Dim dt As DataTable = chklistCauses.DataSource
            Dim strQuery As String = ""
            Dim IsFirstParameter As Boolean = True
            If mstrReportType = "Diagnosis Frequency" Then
                strQuery = "SELECT LtblDiagnosis.Diagnosis, COUNT(tblVisitDiagnosis.DiagnosisId) AS NumberOfUsers," & _
                                        "CAST(MIN(DiagnosisDate) AS VARCHAR) + ' to ' + CAST(MAX(DiagnosisDate) AS VARCHAR) as DateRange " & _
                                        " from tblVisitDiagnosis " & _
                                        "INNER JOIN LtblDiagnosis ON tblVisitDiagnosis.DiagnosisId=LtblDiagnosis.DiagnosisId Where "

                'add where clause
                For i = 0 To dt.Rows.Count - 1
                    If chklistCauses.GetItemChecked(i) = True Then
                        If IsFirstParameter Then
                            strQuery = strQuery & " LtblDiagnosis.DiagnosisId=" & dt.Rows(i).Item("DiagnosisId")
                            IsFirstParameter = False
                        Else
                            strQuery = strQuery & " OR LtblDiagnosis.DiagnosisId=" & dt.Rows(i).Item("DiagnosisId")
                        End If
                    End If
                Next
                If chkViewAll.Checked = True Then
                    strQuery = strQuery & " Group BY LtblDiagnosis.Diagnosis"
                Else
                    strQuery = strQuery & " AND " & " tblVisitDiagnosis.DiagnosisDate  BETWEEN '" & Format(CDate(dteFromDate.EditValue), gstrDtFormat) & " 00:00' AND '" & Format(CDate(dteToDate.EditValue), gstrDtFormat) & " 23:59' Group BY LtblDiagnosis.Diagnosis"
                End If
                Call ShowDiagnosisFrequency(strQuery, Me.ParentForm, "RptDiagnosisFrequency.rpt", "Diagnosis Frequency", IIf(chkViewAll.Checked = True, "", "From " & Format(CDate(dteFromDate.EditValue), gstrDtFormat) & " To " & Format(CDate(dteToDate.EditValue), gstrDtFormat)))

            ElseIf mstrReportType = "Revenue By Diagnosis" Then
                strQuery = "select d.Diagnosis,PaymentDate,sum(Payment) [Amount]" & _
                          " from tblPayment a inner join tblVisits b on (a.PatientId=b.PatientId) " & _
                          " inner join tblVisitDiagnosis c on (b.VisitId=c.VisitId) " & _
                          " inner join LtblDiagnosis d on (c.DiagnosisId=d.DiagnosisId) Where d.DiagnosisId in ("

                'add where clause
                For i = 0 To dt.Rows.Count - 1
                    If chklistCauses.GetItemChecked(i) = True Then
                        If IsFirstParameter Then
                            strQuery = strQuery & dt.Rows(i).Item("DiagnosisId")
                            IsFirstParameter = False
                        Else
                            strQuery = strQuery & "," & dt.Rows(i).Item("DiagnosisId")
                        End If
                    End If
                Next
                If chkViewAll.Checked = True Then
                    strQuery = strQuery & ")"
                Else
                    strQuery = strQuery & ") AND " & " b.VisitDate BETWEEN '" & Format(CDate(dteFromDate.EditValue), gstrDtFormat & " 00:00") & "' AND '" & Format(CDate(dteToDate.EditValue), gstrDtFormat & " 23:59") & " ' "
                End If
                strQuery = strQuery & " Group By PaymentDate,d.Diagnosis"
                Call ShowDiagnosisFrequency(strQuery, Me.ParentForm, "RptRevenueByDiagnosis.rpt", "Revenue By Diagnosis", IIf(chkViewAll.Checked = True, "", "From " & Format(CDate(dteFromDate.EditValue), gstrDtFormat) & " To " & Format(CDate(dteToDate.EditValue), gstrDtFormat)))

            ElseIf mstrReportType = "Revenue By Demographics" Then
                strQuery = "Select Country,Parish,Town,PaymentDate,sum(Payment) [Amount]" & _
                               " from tblPayment a left outer join tblPatientInfo b on (a.PatientId=b.PatientId) " & _
                               " left outer join LtblCountry c on (c.CountryId=b.CountryId) " & _
                               " left outer join LtblParish d on (d.ParishId=b.ParishId) " & _
                               " left outer join LtblTown e on (e.TownId=b.TownId) Where"
                'Dim AgeFrm As Integer = cboFrom.Text
                'Dim AgeTo As Integer = cboTo.Text
                If rgpDemographics.EditValue = 0 Then
                    strQuery = strQuery & " b.CountryId in ("
                ElseIf rgpDemographics.EditValue = 1 Then
                    strQuery = strQuery & " b.ParishId in ("
                ElseIf rgpDemographics.EditValue = 2 Then
                    strQuery = strQuery & " b.TownId in ("
                ElseIf rgpDemographics.EditValue = 3 Then
                    strQuery = strQuery & " b.Occupation in("
                ElseIf rgpDemographics.EditValue = 4 Then
                    strQuery = strQuery & " b.DateOfBirth between '" & Format(DateAdd(DateInterval.Year, -Val(cboTo.Text), DateTime.Now), gstrDtFormat) & "' And '" & Format(DateAdd(DateInterval.Year, -Val(cboFrom.Text), DateTime.Now), gstrDtFormat) & "'"
                End If
                'add where clause
                If rgpDemographics.EditValue = 0 Or rgpDemographics.EditValue = 1 Or rgpDemographics.EditValue = 2 Then
                    For i = 0 To dt.Rows.Count - 1
                        If chklistCauses.GetItemChecked(i) = True Then

                            If IsFirstParameter Then
                                strQuery = strQuery & dt.Rows(i).Item("ID")
                                IsFirstParameter = False
                            Else
                                strQuery = strQuery & "," & dt.Rows(i).Item("ID")
                            End If
                        End If
                    Next
                ElseIf rgpDemographics.EditValue = 3 Then
                    For i = 0 To dt.Rows.Count - 1
                        If chklistCauses.GetItemChecked(i) = True Then

                            If IsFirstParameter Then
                                strQuery = strQuery & "'" & dt.Rows(i).Item("ID") & "'"
                                IsFirstParameter = False
                            Else
                                strQuery = strQuery & ",'" & dt.Rows(i).Item("ID") & "'"
                            End If
                        End If
                    Next
                End If

                If rgpDemographics.EditValue = 0 Or rgpDemographics.EditValue = 1 Or rgpDemographics.EditValue = 2 Or rgpDemographics.EditValue = 3 Then
                    If chkViewAll.Checked = True Then

                        strQuery = strQuery & ")"
                    Else
                        strQuery = strQuery & ") AND " & " a.PaymentDate BETWEEN '" & Format(CDate(dteFromDate.EditValue), gstrDtFormat) & " 00:00' AND '" & Format(CDate(dteToDate.EditValue), gstrDtFormat) & " 23:59' "
                    End If
                ElseIf rgpDemographics.EditValue = 4 Then
                    If chkViewAll.Checked = True Then
                        'strQuery = strQuery & ")"
                    Else
                        strQuery = strQuery & " AND " & " a.PaymentDate BETWEEN '" & dteFromDate.EditValue.ToShortDateString & " 00:00' AND '" & dteToDate.EditValue.ToShortDateString & " 23:59' "
                    End If
                End If
                strQuery = strQuery & " Group by PaymentDate,Country,Parish,Town "
                Call ShowDiagnosisFrequency(strQuery, Me.ParentForm, "RptRevenueByDemographics.rpt", "Revenue By Demographics", IIf(chkViewAll.Checked = True, "", "From " & Format(CDate(dteFromDate.EditValue), gstrDtFormat) & " To " & Format(CDate(dteToDate.EditValue), gstrDtFormat)))

            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message, gstrMessage, MessageBoxButtons.OK, MessageBoxIcon.Error)
            Call AddErrorLog("Error " & Err.Number & " (" & Err.Description & ") in Procedure " & _
                 Reflection.MethodBase.GetCurrentMethod.Name & " Of " & Me.Name)
        End Try
    End Sub

    Private Function IsValidEntry() As Boolean
        'Here check valid entry of fields
        Dim strMsg As String = ""
        IsValidEntry = False
        If rgpDemographics.EditValue <> 4 Then
            Dim NoUserChecked As Boolean = False
            With chklistCauses
                For intCtr As Int16 = 0 To .ItemCount
                    If .GetItemChecked(intCtr) Then
                        NoUserChecked = True
                        Exit For
                    End If
                Next intCtr
            End With
            If NoUserChecked = True Then
                If chkViewAll.Checked = False AndAlso IsDBNullOrNothing(dteFromDate.EditValue) Then
                    strMsg = "Please select From Date"
                    dteFromDate.Focus()
                ElseIf chkViewAll.Checked = False AndAlso IsDBNullOrNothing(dteToDate.EditValue) Then
                    strMsg = "Please select To Date"
                    dteToDate.Focus()
                End If
            Else
                strMsg = "Please select option(s) to view report"
                chklistCauses.Focus()
            End If
        Else
            If IsDBNullOrNothing(cboFrom.Text) Then
                strMsg = "Enter Age from"
                cboFrom.Focus()
            ElseIf Val(cboFrom.Text) < 0 Then
                strMsg = "Enter Proper Age"
                cboFrom.Focus()
            ElseIf IsDBNullOrNothing(cboTo.Text) Then
                strMsg = "Enter Age to"
                cboTo.Focus()
            ElseIf Val(cboTo.Text) < 0 Then
                strMsg = "Enter Proper Age"
                cboTo.Focus()
            End If
        End If
        

        If Trim(strMsg) <> "" Then
            IsValidEntry = False
            MessageBox.Show(strMsg, gstrMessage, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        Else
            IsValidEntry = True
        End If
    End Function

    Private Sub chkViewAll_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkViewAll.CheckedChanged
        If chkViewAll.Checked Then
            dteFromDate.Enabled = False
            dteToDate.Enabled = False
            dteFromDate.Visible = False
            dteToDate.Visible = False
            lblFrom.Visible = False
            lblTo.Visible = False
        Else
            dteFromDate.Enabled = True
            dteToDate.Enabled = True
            dteFromDate.Visible = True
            dteToDate.Visible = True
            lblFrom.Visible = True
            lblTo.Visible = True
        End If
    End Sub

    Private Sub rgpDemographics_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rgpDemographics.SelectedIndexChanged
        If rgpDemographics.EditValue = 0 Or rgpDemographics.EditValue = 1 Or rgpDemographics.EditValue = 2 Or rgpDemographics.EditValue = 3 Then
            grbOption.Visible = True
            grbAgeGroup.Visible = False
            chkSelect.Visible = True
        ElseIf rgpDemographics.EditValue = 4 Then
            grbOption.Visible = False
            chkSelect.Visible = False
            grbAgeGroup.Visible = True
        End If
        If rgpDemographics.EditValue = 0 Then
            Call PopulateCheckedListboxDX(chklistCauses, "Country", "ID", "LtblCountry", "CountryId [ID],Country", "", "Country")
            grbOption.Text = "Select Country"
        ElseIf rgpDemographics.EditValue = 1 Then
            Call PopulateCheckedListboxDX(chklistCauses, "Parish", "ID", "LtblParish", "ParishId [ID],Parish", "", "Parish")
            grbOption.Text = "Select Parish"
        ElseIf rgpDemographics.EditValue = 2 Then
            Call PopulateCheckedListboxDX(chklistCauses, "Town", "ID", "LtblTown", "TownId [ID],Town", "", "Town")
            grbOption.Text = "Select Town"
        ElseIf rgpDemographics.EditValue = 3 Then
            Call PopulateCheckedListboxDX(chklistCauses, "Occupation", "ID", "tblPatientInfo", "Distinct Occupation [ID],Occupation", " Occupation Is Not Null and Occupation <>''", "Occupation")
            grbOption.Text = "Select Occupation"
        ElseIf rgpDemographics.EditValue = 4 Then
            'Call PopulateComboDX(cboFrom, "tblPatientInfo", "Distinct DateDiff(yy,DateOfBirth,getdate()) [Age]", "DateOfBirth Is Not Null", "DateDiff(yy,DateOfBirth,getdate())")
            'Call PopulateComboDX(cboTo, "tblPatientInfo", "Distinct DateDiff(yy,DateOfBirth,getdate()) [Age]", "DateOfBirth Is Not Null", "DateDiff(yy,DateOfBirth,getdate())")
        End If

    End Sub

    Private Sub chkSelect_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkSelect.CheckedChanged
        If chkSelect.Checked = True Then
            
            chklistCauses.CheckAll()
        ElseIf chkSelect.Checked = False Then
            chklistCauses.UnCheckAll()
        End If
    End Sub
End Class
